package org.opens.mybatisplus.controller;

import org.opens.mybatisplus.pojo.result.ResultBean;
import org.opens.mybatisplus.service.JpaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "selectPageController")
@RequestMapping(value = "/select")
public class SelectPageController {

    @Autowired
    private JpaUserService jpaUserService;

    @GetMapping(value = "/page")
    public ResultBean getPage(
            @RequestParam(value = "pageNumber", required = true, defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", required = true, defaultValue = "5") int pageSize,
            @RequestParam(value = "id", required = false, defaultValue = "0") int id,
            @RequestParam(value = "name", required = false, defaultValue = "") String name,
            @RequestParam(value = "old", required = false, defaultValue = "0") int old
    ) {
        return ResultBean.success(jpaUserService.selectByPage(pageNumber, pageSize, id, name, old));
    }

    /**
     * 功能:
     *      -简介
     *          使用mybatis plus快速不写sql构建复杂条件查询(条件嵌套).
     *      -执行的sql
     *          ==>  Preparing: SELECT id,old,name FROM jpa_user WHERE (old < ? AND ( (id IN (?,?,?) OR name LIKE ?) )) LIMIT ?,?
     *          ==> Parameters: 45(Integer), 1(Integer), 2(Integer), 3(Integer), %王%(String), 0(Long), 5(Long)
     *          --说明
     *              查看service中的代码, 会发现: 想要构造出复杂的嵌套条件, 就需要使用java8的lambda方法.
     * @param pageNumber
     * @param pageSize
     * @param ids
     * @param name
     * @param old
     * @return
     */
    @GetMapping(value = "/page2")
    public ResultBean getPage2(
            @RequestParam(value = "pageNumber", required = true, defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", required = true, defaultValue = "5") int pageSize,
            @RequestParam(value = "ids", required = false, defaultValue = "") int[] ids,
            @RequestParam(value = "name", required = false, defaultValue = "") String name,
            @RequestParam(value = "old", required = false, defaultValue = "0") int old
    ) {
        return ResultBean.success(jpaUserService.useQueryWrapperSelectPage(pageNumber, pageSize, ids, name, old));
    }

    /**
     * 功能:
     *      -简介
     *          service使用了lambda用俩选择DO的列字段, 这样就能避免修改实体类需要修改所有service的问题, 降低了程序和DO的依赖性.
     *      -执行的sql
     *          ==>  Preparing: SELECT id,old,name FROM jpa_user WHERE (name LIKE ?) LIMIT ?,?
     *          ==> Parameters: %王%(String), 0(Long), 5(Long)
     *          --说明
     *              查看service层, 会发现不一样的语法.
     *          --注意
     *              这种方式要求在成员变量上打了@TableField注解, 否则无法使用lambda语法.
     * @param pageNumber
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping(value = "/lambda/page")
    public ResultBean selectByLambda(
            @RequestParam(value = "pageNumber", required = true, defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", required = true, defaultValue = "5") int pageSize,
            @RequestParam(value = "name", required = false, defaultValue = "") String name
    ) {
        return ResultBean.success(jpaUserService.selectByNameFromLambda(pageNumber, pageSize, name));
    }

}
